package com.qdxwx.manage.controller.member;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.member.MemberSaleService;
import com.qdxwx.data.service.member.MemberTypeService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.enums.BusinessType;
import com.qdxwx.models.member.MemberType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;

/**
 * 会员类型Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/member/type")
public class MemberTypeEditController extends BaseController {

    @Autowired
    private MemberTypeService memberTypeService;
    @Autowired
    private MemberSaleService memberSaleService;

    /**
     * 新增会员类型
     */
    @GetMapping("/add.html")
    public String add(ModelMap mmap) {
        MemberType memberType = new MemberType();
        mmap.put("memberType", memberType);
        return "member/member-type-edit";
    }

    /**
     * 修改会员类型
     */
    @GetMapping("/edit.html")
    public String edit(HttpServletRequest request, ModelMap mmap) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        if (id < 1) return error404;
        MemberType memberType = memberTypeService.get(id);
        if (memberType == null) return error404;
        mmap.put("memberType", memberType);
        return "member/member-type-edit";
    }

    /**
     * 新增保存会员类型
     */
    @Permissions("member:type:edit")
    @Log(title = "更改会员类型", businessType = BusinessType.INSERT)
    @PostMapping("/save.json")
    @ResponseBody
    public AjaxResult save(HttpServletRequest request) {
        String title = request.getParameter("title");
        if (TKString.isEmpty(title)) return error("请输入标题名称");
        String imageUrl = request.getParameter("imageUrl");
        if (TKString.isEmpty(imageUrl)) return error("请上传会员图片");
        int id = TKConvert.toInteger(request.getParameter("id"));
        MemberType memberType;
        if (id > 0) {
            memberType = memberTypeService.get(id);
            if (memberType == null) return error("找不到指定的会员类型");
        } else {
            memberType = new MemberType();
            memberType.setStatus(0);
        }
        memberType.setTitle(title);
        memberType.setImageUrl(imageUrl);
        int row;
        if (id > 0) {
            row = memberTypeService.update(memberType);
        } else {
            row = memberTypeService.insert(memberType);
        }
        return toAjax(row);
    }

    /**
     * 删除会员类型
     */
    @Permissions("member:type:remove")
    @Log(title = "删除会员类型", businessType = BusinessType.DELETE)
    @PostMapping("/remove.json")
    @ResponseBody
    public AjaxResult remove(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        if (TKString.isEmpty(ids)) return error("参数异常");
        return toAjax(memberTypeService.deleteByIds(ids));
    }

    /**
     * 停用会员与启动会员
     */
    @Permissions("member:type:edit")
    @Log(title = "更改会员类型状态", businessType = BusinessType.UPDATE)
    @PostMapping("/status.json")
    @ResponseBody
    public AjaxResult updateStatus(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        int status = TKConvert.toInteger(request.getParameter("status"));
        if (id < 1) return error("参数异常");
        MemberType memberType = new MemberType();
        memberType.setId(id);
        memberType.setStatus(status);
        int row = memberTypeService.update(memberType);
        if(row > 0){
            memberSaleService.updateStatusByTypeId(id,status);
        }
        return toAjax(row);
    }

}
